﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using Luna小店.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.辅料
{
    public partial class Frm辅料选择 : DevExpress.XtraEditors.XtraForm
    {
        public String STYLE_CATEGORY_CD { get; set; }//款式类别
        public String material_spec { get; set; }//规格
        public String material_id { get; set; }
        public String partId { get; set; }
        public String material_code { get; set; }
        public String partName { get; set; }
        public String material_name_cn { get; set; }
        public List<DtoForChooseContrasts> dtoForChooseContrasts = new List<DtoForChooseContrasts>();//选择的辅料
        public AccessoryItemDTO accessoryItemDTO { get; set; }
        public List<ContrastsDTO> contrastsDTOs = new List<ContrastsDTO>();
        public List<ContrastsLocationDto> contrastsLocationDtos = new List<ContrastsLocationDto>();

        public Frm辅料选择()
        {
            InitializeComponent();
        }

        public Frm辅料选择(String id ,String code,String name, List<DtoForChooseContrasts> dto,String STYLE_CATEGORY_CD)
        {
            this.STYLE_CATEGORY_CD = STYLE_CATEGORY_CD;
            this.dtoForChooseContrasts = dto;
            this.accessoryItemDTO = new AccessoryItemDTO(id, code, name);
            InitializeComponent();
            this.DataSourceContrasts();
            this.gridControl选择的辅料.DataSource = dtoForChooseContrasts;

        }

        private void DataSourceContrasts()
        {
            this.contrastsDTOs = FunctionHelper.JsonDeserialization<List<ContrastsDTO>>(RestCall.httpGetMethod(URL.接口url + "/material/getMaterialData?catelogId=" + this.accessoryItemDTO.id));
            this.searchLookUpEdit辅料.Properties.DataSource = this.contrastsDTOs;
            this.contrastsLocationDtos= FunctionHelper.JsonDeserialization<List<ContrastsLocationDto>>(RestCall.httpGetMethod(URL.接口url + "/acc/getAccessoryPartDTO?category="+this.STYLE_CATEGORY_CD+ "&accessoryItemID="+ this.accessoryItemDTO.id));
            this.searchLookUpEdit位置.Properties.DataSource = this.contrastsLocationDtos;
        }
        #region 选择辅料
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn辅料_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料View_Click(object sender, EventArgs e)
        {
            this.material_name_cn = FunctionHelper.GetgridViewValue(this.searchLookUpEdit辅料View, this.searchLookUpEdit辅料View.FocusedRowHandle, "material_name_cn");
            this.material_code= FunctionHelper.GetgridViewValue(this.searchLookUpEdit辅料View, this.searchLookUpEdit辅料View.FocusedRowHandle, "material_code");
            this.material_id = FunctionHelper.GetgridViewValue(this.searchLookUpEdit辅料View, this.searchLookUpEdit辅料View.FocusedRowHandle, "material_id");
            this.material_spec= FunctionHelper.GetgridViewValue(this.searchLookUpEdit辅料View, this.searchLookUpEdit辅料View.FocusedRowHandle, "material_spec");
            this.textBox规格.Clear();
            this.textBox规格.Text = this.material_spec;
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn辅料_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料.ToolTip = null;
            searchLookUpEdit辅料.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.material_name_cn;
        }
        #endregion

        #region 选择位置
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit位置_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn位置_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit位置View_Click(object sender, EventArgs e)
        {
            this.partName = FunctionHelper.GetgridViewValue(this.searchLookUpEdit位置View, this.searchLookUpEdit位置View.FocusedRowHandle, "partName");
            this.partId = FunctionHelper.GetgridViewValue(this.searchLookUpEdit位置View, this.searchLookUpEdit位置View.FocusedRowHandle, "id");
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn位置_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit位置.ToolTip = null;
            searchLookUpEdit位置.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit位置_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.partName;
        }

        #endregion

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            //if (!String.IsNullOrWhiteSpace(this.material_code) && !String.IsNullOrWhiteSpace(this.material_name_cn) && !String.IsNullOrWhiteSpace(this.numericUpDown辅料数量.Value.ToString()))
            if (!String.IsNullOrWhiteSpace(this.material_name_cn) && !String.IsNullOrWhiteSpace(this.numericUpDown辅料数量.Value.ToString()))
            {
                foreach (DtoForChooseContrasts dto in this.dtoForChooseContrasts)
                {
                    //if (dto.辅料编码.Equals(this.material_code) && dto.辅料位置.Equals(this.partName) && dto.辅料规格.Equals(this.material_spec))
                    if (dto.辅料编码.Equals(this.material_name_cn) && dto.辅料位置.Equals(this.partName) && dto.辅料规格.Equals(this.material_spec))
                    {
                        MessageBox.Show("辅料重复");
                        return;
                    }
                }
                this.dtoForChooseContrasts.Add(new DtoForChooseContrasts(this.material_id, this.material_code, this.material_name_cn, this.partId, this.partName, this.numericUpDown辅料数量.Value.ToString(), this.material_spec));
                this.gridView2.RefreshData();
            }
            else
            {
                MessageBox.Show("请填写完整");
            }
        }

        private void numericUpDown辅料数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((NumericUpDown)sender).Text.Trim().IndexOf('.') > -1)
            {
                if (((NumericUpDown)sender).Text.Trim().Substring(((NumericUpDown)sender).Text.Trim().IndexOf('.') + 1).Length >= 2)//定义小数位
                {
                    if (!char.IsDigit(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName.Equals("count"))
            {
                this.dtoForChooseContrasts[e.RowHandle].辅料单耗 = e.Value.ToString();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < this.dtoForChooseContrasts.Count; i++)
            {
                if(this.dtoForChooseContrasts[i].辅料名称.Equals(FunctionHelper.GetgridViewValue(this.gridView2,this.gridView2.FocusedRowHandle, "辅料名称"))&& this.dtoForChooseContrasts[i].辅料位置.Equals(FunctionHelper.GetgridViewValue(this.gridView2, this.gridView2.FocusedRowHandle, "辅料位置")))
                {
                    this.dtoForChooseContrasts.RemoveAt(i);
                    break;
                }
            }
            this.gridView2.RefreshData();
        }
    }
}
